package fiszki.namespace;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ImageButton;
import android.widget.ListView;

public class SetContentActivity extends Activity {
	private ImageButton addFiszka;
    private ImageButton back;
    private ListView lvFiszki;
    private int setID;
    
    private FiszkiDBAdapter fiszkiDBAdapter;
    private Cursor fiszkiCursor;
    private List<FiszkaTasks> tasks;
    private FiszkaTasksAdapter listAdapter;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getAttributes().format = android.graphics.PixelFormat.RGBA_8888;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.set_content);

        initUiElements();
        initListView();
        initButtonsOnClickListeners(); 
	}
	
    private void initUiElements() { 	
        addFiszka = (ImageButton) findViewById(R.id.buttonAddFiszka);
        back = (ImageButton) findViewById(R.id.buttonBack);
        lvFiszki = (ListView) findViewById(R.id.lvFiszki);
    	setID = getIntent().getExtras().getInt("setID");
    }
    
    private void initButtonsOnClickListeners() { 	 	
    	addFiszka.setOnClickListener(new View.OnClickListener() {
            
			@Override
			public void onClick(View v) {
            	Bundle ID = new Bundle();
            	ID.putInt("setID", setID);
            	Intent newIntent = new Intent(SetContentActivity.this, AddFiszkaActivity.class);
            	newIntent.putExtras(ID);
            	startActivityForResult(newIntent, 0);
            	finish();
            }
        });
    	
    	back.setOnClickListener(new View.OnClickListener() {
            
			@Override
			public void onClick(View v) {
            	finish();
            }
        });
	} 
    
    private void initListView() {
        fillListViewData();
        initListViewOnItemClick();
    }
    
    private void fillListViewData() {
    	fiszkiDBAdapter = new FiszkiDBAdapter(getApplicationContext());
    	fiszkiDBAdapter.open();
        getAllFiszki();
        listAdapter = new FiszkaTasksAdapter(this, tasks);
        listAdapter.notifyDataSetChanged();
        lvFiszki.setAdapter(listAdapter);
        registerForContextMenu(lvFiszki);
    }
    
    private void initListViewOnItemClick() {    	
    	lvFiszki.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
        	
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        		 MenuInflater inflater = getMenuInflater();
        		 inflater.inflate(R.menu.fiszka_menu, menu);
        	}
        });
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	FiszkaTasks task = tasks.get(info.position);
      
    	switch (item.getItemId()) {
      		case R.id.edit_fiszka:
      			Bundle fiszka = new Bundle();
      			fiszka.putInt("ID", task.getId());
      			fiszka.putInt("setID", setID);
      			fiszka.putString("question", task.getQuestion());
      			fiszka.putString("answer", task.getAnswer());
            	
            	Intent newIntent = new Intent(SetContentActivity.this, EditFiszkaActivity.class);
            	newIntent.putExtras(fiszka);
            	startActivityForResult(newIntent, 0);
            	finish();
      			return true;

      		case R.id.delete_fiszka:
      			fiszkiDBAdapter.deleteFiszka(task.getId());
      			updateListViewData();
      			return true;
    	  
      		default:
      			return super.onContextItemSelected(item);
    	}
    }
    
    private void updateListViewData() {
        fiszkiCursor.requery();
        tasks.clear();
        updateFiszkiList();
        listAdapter.notifyDataSetChanged();
    }
    
    private void getAllFiszki() {
        tasks = new ArrayList<FiszkaTasks>();
        fiszkiCursor = getAllEntriesFromDB();
        
        updateFiszkiList();
    }
     
    // OK
    private Cursor getAllEntriesFromDB() {
        fiszkiCursor = fiszkiDBAdapter.getAllFiszki(setID);
        
        if(fiszkiCursor != null) {
            startManagingCursor(fiszkiCursor);
            fiszkiCursor.moveToFirst();
        }
        
        return fiszkiCursor;
    }
    
    private void updateFiszkiList() {
        if(fiszkiCursor != null && fiszkiCursor.moveToFirst()) {
            do {
                int id = fiszkiCursor.getInt(FiszkiDBAdapter.F_ID_COLUMN);
                int setID = fiszkiCursor.getInt(FiszkiDBAdapter.SET_COLUMN);
                String question = fiszkiCursor.getString(FiszkiDBAdapter.QUESTION_COLUMN);
                String answer = fiszkiCursor.getString(FiszkiDBAdapter.ANSWER_COLUMN);

                tasks.add(new FiszkaTasks(id, setID, question, answer));
            } 
            while(fiszkiCursor.moveToNext());
        }
    }
    
    @Override
    protected void onDestroy() {
        if(fiszkiDBAdapter != null)
        	fiszkiDBAdapter.close();
        
        super.onDestroy();
    }
}
